
from odoo import http
from odoo.http import request

from odoo.addons.web.controllers.main import Session, DataSet

from . import model2
from . import session2

import logging
_logger = logging.getLogger(__name__)

Model2 = model2.Model2


# class Session2(Session):
class Session2(http.Controller):
    @http.route('/web2/session/authenticate', type='json', auth="none", cors='*', csrf=False)
    def authenticate2(self, db, login, password, base_location=None):
        _logger.info('call authenticate with %s, %s, %s', db, login, password)

        # session_info = super().authenticate(
        #     db, login, password, base_location=base_location)
        # request.session.rotate = False

        request.session.authenticate(db, login, password)
        request.session.rotate = False
        session_info = request.env['ir.http'].session_info()
        return self._patch_session_info(session_info)

    @http.route('/web2/session/get_session_info', type='json', auth="user", cors='*', csrf=False)
    def get_session_info2(self):
        # session_info = super().get_session_info()
        session_info = request.env['ir.http'].session_info()
        _logger.info('call get_session_info ')

        return self._patch_session_info(session_info)

    @http.route('/web2/session/check', type='json', auth="user", cors='*', csrf=False)
    def check2(self):
        # return super().check()
        pass
        return None

    @http.route('/web2/session/destroy', type='json', auth="user", cors='*', csrf=False)
    def destroy2(self):
        # return super().destroy()
        # # 安装销售模块后, utm 模块 会找 db
        request.session.logout(keep_db=True)

    def _patch_session_info(self, session_info):
        return session2._patch_session_info(session_info)


class DataSet2(DataSet):

    @http.route('/web2/dataset/search_read', type='json', auth="user", cors='*', csrf=False)
    def search_read2(self, model, fields=False, offset=0, limit=False, domain=None, domain2=None, sort=None):
        _logger.info('call search_read with model:%s, domain:%s, domain2:%s, fields:%s, offset:%s, limit:%s, sort:%s',
                     model, domain, domain2, fields, offset, limit, sort)

        model2 = Model2(self.do_search_read, model)
        return model2.web_search_read2(fields, offset, limit, domain, domain2, sort)

    @http.route(['/web2/dataset/call_kw',
                 '/web2/dataset/call_kw/<path:path>'], type='json', auth="user", cors='*', csrf=False)
    def call_kw2(self, model, method, args, kwargs, path=None):
        _logger.info('call call_kw with model:%s, method:%s, args:%s, kwargs:%s, path:%s',
                     model, method, args, kwargs, path)

        if not hasattr(Model2, method):
            print('hasattr,no, method', method, request.env.context)
            print('hasattr,no, method, user_cntext:',
                  method, kwargs.get('context') or {})

            context = kwargs.get('context') or {}
            context.update(request.env.context)
            kwargs.update({'context': context})

            return self._call_kw(model, method, args, kwargs)

        print('hasattr, in method', method)
        context = kwargs.get('context') or {}
        model2 = Model2(self._call_kw, model, context)

        return getattr(model2, method)(*args, **kwargs)
